<?php
namespace app\admin\model;

use think\Model;
use think\db;

class ApplicationForMoney extends Model
{
    /**
     *用款信息审核操作
     * @param $id 项目ID
     * @param $systerm_check 审核状态
     * @param $systerm_msg 审核意见
     * @author hey_heyang(635870453@qq.com)
     * @return 返回结果，status结果(1:成功; 0:失败),msg存放错误信息
     */
    public function confirmDetailDivide($id,$systerm_check,$systerm_msg){
        $result['status'] = 1;
        $result['msg'] = '操作成功';
        $project_info = $this->where(array('id'=>$id))->find()->toArray();
        if($project_info){
            if($systerm_check==1){  //风控审核通过
                $this->where(array('id'=>$id))->update(array('status'=>2,'systerm_msg'=>$systerm_msg,'systerm_check_time'=>time()));
                companyTobeNotice($project_info['company_id'], '用款', '您发起的金额为'.$project_info['money'].'的申请已通过!');
            }elseif($systerm_check==2){ // 风控审核不通过
                $this->where(array('id'=>$id))->update(array('status'=>3,'systerm_msg'=>$systerm_msg,'systerm_check_time'=>time()));
                companyTobeNotice($project_info['company_id'], '用款', '您发起的金额为'.$project_info['money'].'的申请已被平台拒绝!');
                $result['msg'] = '平台已拒绝';
            }
        }else{
            $result['status']=0;
            $result['msg'] = '未查询到用款申请信息';
        }
        return $result;

    }

    /**
     *根据认投项目id 获取用款申请列表
     * @param $id 认投项目ID
     * $status 获取用款申请的状态
     * @author hey_heyang(635870453@qq.com)
     * @return array()
     */

    public function getAfmInfo($id,$where){
        if($id>0){
            $pModel= new Project();
            $company_id = $pModel->where(array('id'=>$id))->value('initiator');
            $where['company_id'] = $company_id;
            $where['project_id'] = $id;
            $res = $this->where($where)->order('id asc')->select();
        }

        return empty($res)?array():$res->toArray();
    }


    /**
     *根据认投项目id 获取用款申请列表
     * @param $afm_id 用款申请ID
     * @param $series_number 匹配的申请期数
     * @param $order_ids 关联的订单id
     * @param $money  申请的用款金额
     * @param $project_id 认投项目ID
     * @author hey_heyang(635870453@qq.com)
     * @return array()
     */
    public function bindProjectOrder($afm_id,$series_number,$order_ids,$money,$project_id){
        $orModel =new Order();
        $result['status'] = 1;
        $result['msg'] = '操作成功';
        if(!empty($order_ids)){
            $where['id'] =array('in',$order_ids);
            $res = $this->updateAfmData($afm_id,$series_number,$where);
        }else{
           $querydata=$orModel->getMostOrder($project_id,$money);
           if($querydata['orderids']){
               $where['id'] =array('in',$querydata['orderids']);
               $res=$this->updateAfmData($afm_id,$series_number,$where);
           }
        }
        if(!$res){
            $result['status']=0;
            $result['msg'] = '操作失败';
        }

        return $result;

    }


    /**
     *根据认投项目id 获取用款申请列表
     * @param $afm_id 用款申请ID
     * @param $series_number 匹配的申请期数
     * @param $where 更新添加
     * @author hey_heyang(635870453@qq.com)
     * @return true/false
     */
    public function updateAfmData($afm_id,$series_number,$where){
        $orModel =new Order();
        Db::startTrans();
        $or_res = $orModel->where($where)->update(array('relevance_id'=>$afm_id,'series_number'=>$series_number));
        $order_amount = $orModel->where(array('relevance_id'=>$afm_id,'series_number'=>$series_number))->sum('money');
        if($or_res){
            $afm_res = $this->where(array('id'=>$afm_id))->update(array('relevance_status'=>2,'real_money'=>$order_amount));
        }
        if($afm_res){
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }
    }



}